package lec09.pm;

import java.util.Map;
import java.util.Random;
import java.util.TreeMap;
import java.util.Vector;

public class Graph {
	static Random random = new Random();

	public Graph(int n, double d) {
		adj = new TreeMap<>();
		for (int i = 1; i <= n; i++)
			adj.put(i, new Vector<Integer>());
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++) {
				if (Math.random() < d) {
					adj.get(i).add(j);
				}
			}
	}

	public Graph(int i) {
		this(i, 0.8);
	}

	Map<Integer, Vector<Integer>> adj;

	@Override
	public String toString() {
		return adj.toString();
	}

	public void addEdge(int i, int j) {
		if (adj.containsKey(i)&&adj.get(i).contains(j))
			return;
		adj.get(i).add(j);

	}

	public void delEdge(int i, int j) {
		if (adj.containsKey(i)&&adj.get(i).contains(j))
			adj.get(i).remove(j);

	}

	public void delVertex(int i) {
		for(Integer n:adj.keySet())
			delEdge(i,n);
		for(Integer n:adj.keySet())
			delEdge(n,i);
		adj.remove(i);

	}
}
